/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.web.baseData;

import java.util.ArrayList;
import java.util.List;

import com.javaeye.chenweihuacwh.dto.baseData.ProductCategory;
import com.javaeye.chenweihuacwh.service.baseData.IProductCategoryService;
import com.opensymphony.xwork2.ActionSupport;

public class ProductCategoryAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1998966732318406119L;

	private IProductCategoryService categoryService;
	
	private List<ProductCategory> categoryList;
	
	private ProductCategory category;
	
	private String categoryId;
	
	public ProductCategoryAction() {
		
	}
	
	public String queryCategoryJOSN() throws Exception {
		categoryList = categoryService.getCategorys(categoryId);
		return SUCCESS;
	}
	
	/**
	 *根据父分类ID取得子分类
	 */
	public String queryCategoryList() throws Exception {
		if ("0".equals(categoryId)) {
			//根节点，没有父节点信息
			category = new ProductCategory();
			category.setParentId(-1);
		} else {
			category = categoryService.getCategory(categoryId);
		}
		categoryList = categoryService.getCategorys(categoryId);
		return SUCCESS;
	}
	
	public String queryForAddCategory() throws Exception {
		return SUCCESS;
	}
	
	public String querySingleCategory() throws Exception {
		category = categoryService.getCategory(categoryId);
		return SUCCESS;
	}
	
	public String saveCategory() throws Exception {
		category = categoryService.saveCategory(category);
		categoryId = String.valueOf(category.getParentId());
		return queryCategoryList();
	}
	
	public String deleteCategory() throws Exception {
		category = categoryService.getCategory(categoryId);
		boolean success = categoryService.removeCategory(categoryId);
		categoryId = String.valueOf(category.getParentId());
		queryCategoryList();
		if (!success) {
			List<Object> params = new ArrayList<Object>();
			params.add("分类");
			this.addActionError(getText("actionErrorDeleteFK", params));
			return INPUT;
		}
		return SUCCESS;
	}
	
	public void setCategoryService(IProductCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public List<ProductCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<ProductCategory> categoryList) {
		this.categoryList = categoryList;
	}

	public ProductCategory getCategory() {
		return category;
	}

	public void setCategory(ProductCategory category) {
		this.category = category;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
}
